package com.itheima.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Reference;

import com.itheima.log.LogAnno;
import com.itheima.pojo.Result;
import com.itheima.service.ReportMemberService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.joda.time.Months;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/report")
public class ReportMemberController {
    @Reference
    ReportMemberService reportMemberService;


//    @LogAnno("查询会员报表")
    @PreAuthorize("hasAnyAuthority('REPORT_VIEW')")
    @RequestMapping("/getMemberReport")
    public Result getMemberReport(@RequestParam String value6, HttpServletRequest request) throws ParseException {

        String keyword = null;
        try {
            keyword = new String(value6.getBytes("ISO-8859-1"), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        String[] split = keyword.split(",");
        SimpleDateFormat sf = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss z",Locale.ENGLISH);
        Date d1 = sf.parse(split[0]);
//        Date date1 = new Date(split[0]);
        String formatDate1 =DateFormat.getDateInstance().format(d1);
//        Date date2 = new Date(split[1]);
        Date d2 = sf.parse(split[1]);
        String formatDate2 =DateFormat.getDateInstance().format(d2);
        DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
        org.joda.time.DateTime start = formatter.parseDateTime(formatDate1);
        org.joda.time.DateTime end= formatter.parseDateTime(formatDate2);

        //end-start 结果可为负数、正数、0
        int esMonths = Months.monthsBetween(start, end).getMonths();

        int abs = Math.abs(esMonths);
        DateTime dateTime = DateUtil.offsetMonth(d2, -abs);
        Map<String,List> map = new HashMap<>();
        List<String> months = new ArrayList();
        List<Integer> memberCount = new ArrayList();
        for (int i = 0; i < abs+1; i++) {
            DateTime dateTime1 = DateUtil.offsetMonth(dateTime,i);
            String month = dateTime1.toString("yyyy-MM");
            int count = reportMemberService.getMemberCount(month);
            memberCount.add(count);
            months.add(month);
        }
        map.put("months", months);
        map.put("memberCount", memberCount);
        return Result.success("", map);
    }
    public static String parseTime(String datdString) {
        datdString = datdString.replace("GMT", "").replaceAll("\\(.*\\)", "");
        //将字符串转化为date类型，格式2016-10-12
        SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z", Locale.ENGLISH);
        Date dateTrans = null;
        try {
            dateTrans = format.parse(datdString);
            return new SimpleDateFormat("yyyy-MM").format(dateTrans).replace("-","/");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return datdString;

    }

//    public static void main(String[] args) {
//
//        Date date1 = new Date("Thu Mar 07 2019 12:00:00 GMT+0800 (中国标准时间) ");
//        String formatDate1 =DateFormat.getDateInstance().format(date1);
//        Date date2 = new Date("Thu Mar 07 2030 12:00:00 GMT+0800 (中国标准时间) ");
//        String formatDate2 =DateFormat.getDateInstance().format(date2);
//        DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
//        org.joda.time.DateTime start = formatter.parseDateTime(formatDate1);
//        org.joda.time.DateTime end= formatter.parseDateTime(formatDate2);
//
//        //end-start 结果可为负数、正数、0
//        int esMonths = Months.monthsBetween(start, end).getMonths();
//        System.out.println("esMonths = " + esMonths);
//    }


}
